package com.yanchangapp.ui.activity.purchase;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yanchangapp.R;
import com.yanchangapp.adapter.POrderCntAdapter;
import com.yanchangapp.model.bean.POrderCntListBean;
import com.yanchangapp.ui.activity.mail.MailSearchActivity;
import com.yanchangapp.ui.presenter.OrderCntAtPresenter;
import com.yanchangapp.ui.view.OrderCntAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.util.window.KeyboardHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 采购 -- 委托企业 -- 合同列表页面...
 */
public class POrderCntListActivity extends BaseActivity<OrderCntAtView, OrderCntAtPresenter> implements OrderCntAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.rvContract)
    RecyclerView rvContract;
    @BindView(R.id.etSearchText)
    EditText etSearchText;
    @BindView(R.id.ivDelete)
    ImageView ivDelete;
    @BindView(R.id.tvCancel)
    TextView tvCancel;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.llEmptyMail)
    LinearLayout llEmptyMail;
    private String  mCurrentKeywords;
    private List<POrderCntListBean> mPOrderCntListBeanList = new ArrayList<>();

    private POrderCntAdapter mPOrderCntAdapter;
    @Override
    protected void init() {

    }

    public static void show(Activity activity) {
        Intent intent = new Intent(activity, POrderCntListActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_porder_cnt_list;
    }

    @Override
    protected OrderCntAtPresenter createPresenter() {
        return new OrderCntAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("采购合同列表");
        initAdapter();
        initRefresh();
    }

    private void initRefresh() {
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshData();
            }
        });
    }

    private void initAdapter() {
        rvContract.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mPOrderCntAdapter = new POrderCntAdapter(R.layout.item_order_contract_list,mPOrderCntListBeanList);
        // 设置adapter
        rvContract.setAdapter(mPOrderCntAdapter);
    }

    @Override
    protected void initData() {
        refreshData();
        // 自动刷新不好看啊
        // refreshLayout.autoRefresh();
    }

    // 数据刷新
    private void refreshData(){
        mPresenter.queryCntList(mCurrentKeywords);
    }

    private void stateContent(){
        refreshLayout.setEnableRefresh(true);
        if (llEmptyMail.getVisibility() == View.VISIBLE){
            llEmptyMail.setVisibility(View.GONE);
        }

        if (refreshLayout.getVisibility() == View.GONE){
            refreshLayout.setVisibility(View.VISIBLE);
        }

        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }
    }

    private void stateError(){
        refreshLayout.setEnableRefresh(false);
        if (llEmptyMail.getVisibility() == View.VISIBLE){
            llEmptyMail.setVisibility(View.GONE);
        }

        if (refreshLayout.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }

        if (llErrorNetwork.getVisibility() == View.GONE){
            llErrorNetwork.setVisibility(View.VISIBLE);
        }
    }

    // 设置选中的状态
    private void stateEmpty(){
        refreshLayout.setEnableRefresh(false);
        if (llEmptyMail.getVisibility() == View.GONE){
            llEmptyMail.setVisibility(View.VISIBLE);
        }

        if (refreshLayout.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }

        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> {
            finish();
        });


        tvCancel.setOnClickListener(v -> {
            KeyboardHelper.hideKeyboard(tvCancel);
            etSearchText.setText("");
        });

        ivDelete.setOnClickListener(v -> etSearchText.setText(""));

        etSearchText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            /**
             * 当搜索框的文字变化时回调此方法
             * @param s
             * @param start
             * @param before
             * @param count
             */
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mCurrentKeywords = s.toString();
                if (s.toString().length() == 0) {
                    ivDelete.setVisibility(View.GONE);
                } else {
                    ivDelete.setVisibility(View.VISIBLE);
                }
                refreshData();
                // 刷新页面....
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        btnNextRefresh.setOnClickListener(v -> {
            refreshLayout.setEnableRefresh(true);
            // 感觉自动刷新不好看
            // refreshLayout.autoRefresh();
            refreshData();
        });


        /*
           item按钮的点击事件...
         */
        mPOrderCntAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {
            POrderCntListBean bean = mPOrderCntListBeanList.get(i);
            POrderCtCntActivity.show(POrderCntListActivity.this,
                    bean.getId(),bean.getSupplycoId(),bean.getSupName(),bean.getInvoiceNo());
        });
    }

    private boolean hasContent(){
        return (mPOrderCntListBeanList != null) && (mPOrderCntListBeanList.size() != 0);
    }

    @Override
    public void queryCntListSuccess(List<POrderCntListBean> mLists) {
        if (refreshLayout.getState() == RefreshState.Refreshing){
            refreshLayout.finishRefresh();
        }


        mPOrderCntListBeanList.clear();
        if (mLists != null){
            mPOrderCntListBeanList.addAll(mLists);
        }

        if (hasContent()){
            stateContent();
        }else {
            stateEmpty();
        }

        mPOrderCntAdapter.notifyDataSetChanged();
    }

    @Override
    public void queryCntListFailure(String err) {
        if (!TextUtils.isEmpty(err)){
            UIUtils.showToast(err);
        }

        if (hasContent()){
            stateContent();
        }else {
            stateError();
        }
    }
}
